package com.king.netty.protocoltcp;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.handler.codec.MessageToMessageDecoder;
import io.netty.handler.codec.ReplayingDecoder;

import java.util.List;

public class MyMessageDecoder extends ReplayingDecoder<MessageProtocol> {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        System.out.println("MyMessageDecpder decode 被调用");

        // 需要将得到的二进制字节码 -> MessageProtocol 数据包（对象）
        int len = in.readInt();
        byte[] bytes = new byte[len];
        in.readBytes(bytes);

        // 封装城 MessageProtocol 对象，放入 out, 传递下一个handler业务处理
        out.add(new MessageProtocol(len, bytes));
    }
}
